import {
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor,
} from '@nestjs/common';
import { map, Observable, tap } from 'rxjs';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const url = context?.switchToHttp()?.getRequest()?.url;
    const now = Date.now();
    if (process.env.NODE_ENV === 'development') {
      return next
        .handle()
        .pipe(
          map((data) => {
            console.log('返回数据', data);
            return data;
          }),
        )
        .pipe(tap(() => console.log(`${url}:${Date.now() - now}ms`)));
    } else {
      return next.handle();
    }
  }
}
